/**
 * 
 */
package dms.presentation.action.supervisor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.dom4j.Document;
import org.dom4j.Element;

import dms.dto.BulletinDTO;
import dms.dto.NoticeDTO;
import dms.model.Building;
import dms.model.form.Bulletin;
import dms.model.form.Notice;
import dms.model.role.DormSupervisor;
import dms.model.role.wrapper.DormSupervisorWrapper;
import dms.model.util.PageIterator;
import dms.service.BulletinQueryService;
import dms.service.NoticeQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;
import dms.util.XMLUtil;

/**
 * @author 李健
 *
 */
public class BuildingWorkAction extends MappingDispatchAction {
	
	private static final Log log = LogFactory.getLog(BuildingWorkAction.class);
	
	public ActionForward bulletinList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Integer start = null;
		Integer count = null;
		int[] params = null;
		
		BulletinQueryService bulletinQueryService = (BulletinQueryService) ContextUtil.getContext().getBean("bulletinQueryService");
		Building building = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		PageIterator<Bulletin> bulletins = null;
		try {
			building = dormSupervisorWrapper.getDormSupervisor().getBuilding();
			params = PageUtil.parsePageParameter(request);
			
			start = params[0];
			count = params[1];
			
			bulletins = bulletinQueryService.findByBuilding(start, count, null, null, building, null);
			request.setAttribute("bulletins", bulletins);
			request.setAttribute("dormSupervisor", dormSupervisorWrapper.getDormSupervisor());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
			
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addBulletinForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = wrapper.getDormSupervisor();
		request.setAttribute("dormSupervisor", dormSupervisor);

		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addBulletin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BulletinDTO dto = (BulletinDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		
		try {
		
			dormSupervisorWrapper.addBulletin(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception("增加公告失败，原因：缺少某些数据");
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateBulletin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		BulletinDTO bulletinDTO = (BulletinDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		
		/*//如果有错误跳回
		ActionErrors errors = bulletinDTO.validate(mapping, request);
		if (errors != null || !errors.isEmpty()) {
			response.sendRedirect("updateBulletin.do?id=" + bulletinDTO.getId());
			return null;
		}*/
		
		try {
			dormSupervisorWrapper.updateBulletin(bulletinDTO);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception("无法更新公告");
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateBulletinForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long bulletinId = null;
		Bulletin bulletin = null;
		BulletinQueryService bulletinQueryService = (BulletinQueryService) ContextUtil.getContext().getBean("bulletinQueryService");
		try {
			bulletinId = Long.valueOf(request.getParameter("id"));
			bulletin = bulletinQueryService.findById(bulletinId);
			request.setAttribute("bulletin", bulletin);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward deleteBulletin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long bulletinId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element bulletinElement = document.addElement("response").addElement("bulletin");
		
		try {
			bulletinId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.deleteBulletinById(bulletinId);
			bulletinElement.addElement("status").addText("true");
			bulletinElement.addElement("message").addText("公告ID：" + bulletinId + "删除成功!");
		} catch (NumberFormatException e) {
			log.error(e.getMessage());
			bulletinElement.addElement("status").addText("false");
			bulletinElement.addElement("message").addText(e.getMessage());
		} catch (Exception e) {			
			log.error(e.getMessage());
			bulletinElement.addElement("status").addText("false");
			bulletinElement.addElement("message").addText(e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(bulletinElement.asXML());
		return null;
	}

	
	public ActionForward lostNoticeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int[] params = null;
		int start = 0;
		int count = 0;
		PageIterator<Notice> lostNotices = null;
		NoticeQueryService noticeQueryService = (NoticeQueryService) ContextUtil.getContext().getBean("noticeQueryService");
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Building building = wrapper.getDormSupervisor().getBuilding();
		try {
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			lostNotices = noticeQueryService.findLostNoticeByBuilding(start, count, building, null, null, null, null);
			request.setAttribute("notices", lostNotices);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward foundNoticeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int[] params = null;
		int start = 0;
		int count = 0;
		PageIterator<Notice> foundNotices = null;
		NoticeQueryService noticeQueryService = (NoticeQueryService) ContextUtil.getContext().getBean("noticeQueryService");
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Building building = wrapper.getDormSupervisor().getBuilding();
		
		try {
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			foundNotices = noticeQueryService.findLostNoticeByBuilding(start, count, building, null, null, null, null);
			request.setAttribute("foundNotices", foundNotices);
			
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception(e.getMessage());
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addLostNoticeForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setAttribute("dormSupervisor", getCurrentDormSupervisor());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addFoundNoticeForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("dormSupervisor", getCurrentDormSupervisor());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addFoundNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NoticeDTO notice = (NoticeDTO) form;
		
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		try {
			dormSupervisorWrapper.addFoundNotice(notice);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addLostNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NoticeDTO notice = (NoticeDTO) form;
		
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		try {
			dormSupervisorWrapper.addLostNotice(notice);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward deleteNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long noticeId = null;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		String message = null;
		Boolean status = null;
		try {
			noticeId = Long.valueOf(request.getParameter("id"));
			wrapper.deleteNoticeById(noticeId);		
			message = "删除成功!";
			status = true;
		} catch (Exception e) {
			status = false;
			message = "删除失败,原因可能是:" + e.getMessage();
		}
		
		Document document = createNoticeXML(status, message);
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}
	
	public ActionForward updateNoticeForm(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long noticeId = null;
		NoticeQueryService noticeQueryService = (NoticeQueryService) ContextUtil.getContext().getBean("noticeQueryService");
		Notice notice = null;
		Document document = null;
		try {
			noticeId = Long.valueOf(request.getParameter("id"));
			notice = noticeQueryService.findNoticeById(noticeId);
			request.setAttribute("notice", notice);
			document = createNoticeXML(true, "已获取启示ID:" + noticeId + "的信息");
			
		} catch (Exception e) {
			document = createNoticeXML(false, e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}
	
	public ActionForward updateNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NoticeDTO noticeDTO = (NoticeDTO) form;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = null;
		
		try {
			wrapper.updateNotice(noticeDTO);
			document = createNoticeXML(true, "更新启示成功！");
		} catch (Exception e) {
			document = createNoticeXML(false, "更新失败,原因可能是:" + e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}
	
	/**
	 * 获得当前会话的supervisor
	 * @return
	 */
	private DormSupervisor getCurrentDormSupervisor() {
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = dormSupervisorWrapper.getDormSupervisor();
		return dormSupervisor;
	}
	
	private Document createNoticeXML(Boolean status, String message) {
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element noticeElement = document.addElement("response").addElement("notice");
		noticeElement.addElement("status").addText(status.toString());
		noticeElement.addElement("message").addText(message);
		return document;
	}
	
}
